<%@ page import="com.example.fruitdb.controller.FruitServlet" %>
<%@ page import="com.example.fruitdb.service.FruitService" %>
<%@ page import="java.util.Vector" %>
<%@ page import="com.example.fruitdb.entity.Fruit" %>
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8" %>
<!DOCTYPE html>
<html>
<head>
    <meta content="text/html;charset=utf-8">
    <title>陈骏蔚的水果铺子</title>
    <style>
        fieldset {
            padding: 0;
            border: 0;
            margin-top: 25px;
        }

        .ui-dialog .ui-state-error {
            padding: .3em;
        }
    </style>

</head>
<body>
<h1 align="center">陈骏蔚的水果铺子</h1>
<br/>


<%
    if (session.getAttribute("login") != null) {
        out.println("<h1 align=\"center\">欢迎管理员</h1>");
    } else out.println("<p style=\"text-align: center\"><a href=\"admin.jsp\">管理员登录</a></p>");
%>

<div style="text-align: center">
    <form action="queryFruit" id="queryForm" method="post" accept-charset="utf-8">
        水果名：<input type="text" id="ufruitname" name="fruitName">
        <input type="submit" id="btn_query" name="submit" value="搜索">
        <%
            if (session.getAttribute("login") != null) {
                out.println("<input type=\"button\" id=\"btn_insert\" name=\"insert\" value=\"新增\">");
            }
        %>

    </form>
    <table border="1px" style="margin: auto">
        <tr>

            <td>序号</td>
            <td>名称</td>
            <td>价格</td>
            <td>数量</td>
            <td>描述</td>
            <%
                if (session.getAttribute("login") != null) {
                    out.println("<td>操作</td>");
                }
            %>
        </tr>


        <%
            FruitServlet fruitServlet = new FruitServlet();

            Vector<Fruit> ffruits = new Vector<Fruit>();
            ffruits = (Vector<Fruit>) session.getAttribute("fruits");
            if (ffruits == null) {

            } else {

                for (Fruit f : ffruits) {
                    out.println("<tr>");

                    out.println("<td>" + f.getFid() + "</td>");
                    out.println("<td>" + f.getFname() + "</td>");
                    out.println("<td>" + f.getPrice() + "</td>");
                    out.println("<td>" + f.getFcount() + "</td>");
                    out.println("<td>" + f.getRemark() + "</td>");

                    if (session.getAttribute("login") != null) {
                        out.println("<td><a style=\"color:blue;cursor:pointer;\" onclick=\"updateRow(this);\">修改<a>&nbsp<a style=\"color:blue;cursor:pointer;\" onclick=\"delRow(this);\">删除</a></td>");
                    }
                    out.println("</tr>");
                }
            }
        %>


    </table>

</div>

<div id="modify-Form" title="修改水果信息">
    <p class="validateTips">所有的表单字段都是必填的。</p>

    <form action="modifyFruit" method="post" id="modifyForm">
        <input type="hidden" name="fid" id="fid">
        水果名: <input type="text" name="fname" id="fname"><br>
        价格: <input type="text" name="fprice" id="fprice"><br>
        数量: <input type="text" name="fcount" id="fcount"><br>
        描述: <input type="text" name="fremark" id="fremark"><br>
        <span id="warningMsg" style="font-size: 12px;color: red"></span><br>
    </form>
</div>
<div id="insert-Form" title="新增水果">
    <p class="validateTips">所有的表单字段都是必填的。</p>

    <form action="insertFruit" method="post" id="insertForm">
        水果名: <input type="text" name="fname" id="iname"><br>
        价格: <input type="text" name="fprice" id="iprice"><br>
        数量: <input type="text" name="fcount" id="icount"><br>
        描述: <input type="text" name="fremark" id="iremark"><br>
        <span id="inswarningMsg" style="font-size: 12px;color: red"></span><br>
    </form>
</div>

</body>

<link rel="stylesheet" type="text/css" href="css/jquery-ui.css">
<script type="text/javascript" src="js/jquery-3.6.0.js"></script>
<script type="text/javascript" src="js/jquery-ui.js"></script>

<script type="text/javascript">
    $("#modify-Form").dialog({
        autoOpen: false,
        height: 300,
        width: 350,
        modal: true,

        buttons: {
            "确认": function () {
                if (isEmpty($("#fname").val())||isEmpty($("#fprice").val())||isEmpty($("#fcount").val())||isEmpty($("#fremark").val()))
                {
                    $("#warningMsg").html("请填写完整信息");
                    return;
                }
                else
                {
                    alert($("#modifyForm").serialize());
                    //$("#modifyForm").submit();
                    $.ajax({
                        url: "modifyFruit",
                        type: "post",
                        //contentType: "application/x-www-form-urlencoded;charset=UTF-8",
                        data: $("#modifyForm").serialize(),
                        success: function (data) {
                            if (data == "success")
                            {
                                alert("修改成功");
                                window.location.reload();
                            }
                            else
                            {
                                alert("修改失败");
                            }
                        }
                    });
                }

            },
            "取消": function () {
                $(this).dialog("close");
            }
        }


    });
    $("#insert-Form").dialog({
        autoOpen: false,
        height: 300,
        width: 350,
        modal: true,

        buttons: {
            "确认": function () {
                if (isEmpty($("#iname").val())||isEmpty($("#iprice").val())||isEmpty($("#icount").val())||isEmpty($("#iremark").val()))
                {
                    $("#inswarningMsg").html("请填写完整信息");
                    return;
                }
                else
                {
                    alert($("#insertForm").serialize());
                    //$("#insertFruit").submit();
                    $.ajax({
                        url: "insertFruit",
                        type: "post",
                        //contentType: "application/x-www-form-urlencoded;charset=UTF-8",
                        data: $("#insertForm").serialize(),
                        success: function (data) {
                            if (data == "success")
                            {
                                alert("新增水果成功");
                                window.location.reload();
                            }
                            else
                            {
                                alert("新增水果失败");
                            }
                        }
                    });
                }

            },
            "取消": function () {
                $(this).dialog("close");
            }
        }


    });

    $("#btn_query").click(function () {
        var ufruitname = $("#ufruitname").val();
        if (isEmpty(ufruitname)) {
            var form = document.createElement("form");
            form.action = "queryFruit";
            form.method = get;
            form.submit();
            return;
        }
        $("#queryForm").submit();
    });


    //删除水果
    function delRow(obj) {
        var fruitId = $(obj).parent().parent().children().eq(0).text();
        var fruitName = $(obj).parent().parent().children().eq(1).text();
        var fruitPrice = $(obj).parent().parent().children().eq(2).text();
        var fruitCount = $(obj).parent().parent().children().eq(3).text();
        var fruitRemark = $(obj).parent().parent().children().eq(4).text();
        var fruit = {
            "fruitId": fruitId,
            "fruitName": fruitName,
            "fruitPrice": fruitPrice,
            "fruitCount": fruitCount,
            "fruitRemark": fruitRemark
        };
        //var fruitJson = JSON.stringify(fruit);
        $.ajax({
            url: "deleteFruit",
            type: "post",
            //contentType: "application/x-www-form-urlencoded;charset=UTF-8",
            data: {
                fruit
            },
            success: function (data) {
                if (data == "success") {
                    alert("删除成功");
                    window.location.reload();
                } else {
                    alert("删除失败");
                }
            }
        });
    }


    //确认要更改的水果信息
    function updateRow(obj) {
        var fruitId = $(obj).parent().parent().children().eq(0).text();
        var fruitName = $(obj).parent().parent().children().eq(1).text();
        var fruitPrice = $(obj).parent().parent().children().eq(2).text();
        var fruitCount = $(obj).parent().parent().children().eq(3).text();
        var fruitRemark = $(obj).parent().parent().children().eq(4).text();
        $("#fid").val(fruitId);
        $("#fname").val(fruitName);
        $("#fprice").val(fruitPrice);
        $("#fcount").val(fruitCount);
        $("#fremark").val(fruitRemark);
        $("#modify-Form").dialog("open");
    }

    function isEmpty(str) {
        return str == null || str.trim() == "";
    }
    $("#btn_insert").click(
        function () {
            $("#insert-Form").dialog("open");
        }
    )
</script>
</html>
